<template>
  <div class="account-settings-info-view">
    <a-row :gutter="16">
      <a-col :md="24" :lg="16">
        <a-form layout="vertical" :form="form">
          <a-form-item label="真实姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入真实姓名" v-decorator="[ 'realname', validatorRules.realname]" />
          </a-form-item>
          <a-form-item label="头像" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-upload
              v-decorator="['avatar']"
              :fileType="'image'"
              :number="1"
              :trigger-change="true"
            ></j-upload>
          </a-form-item>

          <a-form-item label="生日" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-date-picker
              style="width: 100%"
              placeholder="请选择生日"
              v-decorator="['birthday', {initialValue:!userInfo.birthday?null:moment(userInfo.birthday,dateFormat)}]"
            />
          </a-form-item>

          <a-form-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-select v-decorator="[ 'sex', {}]" placeholder="请选择性别">
              <a-select-option :value="1">男</a-select-option>
              <a-select-option :value="2">女</a-select-option>
            </a-select>
          </a-form-item>

          <a-form-item label="邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入邮箱" v-decorator="[ 'email', validatorRules.email]" />
          </a-form-item>

          <a-form-item label="手机号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入手机号码" v-decorator="[ 'phone', validatorRules.phone]" />
          </a-form-item>
          <a-form-item>
            <a-button type="primary" @click="handleSubmit">提交</a-button>
          </a-form-item>
        </a-form>
      </a-col>
    </a-row>

    <avatar-modal ref="modal"></avatar-modal>
  </div>
</template>

<script>
import Vue from 'vue'
import AvatarModal from './AvatarModal'
import JUpload from '@/components/jeecg/JUpload'
import moment from 'moment'
import pick from 'lodash.pick'
import { getAction,putAction } from '@/api/manage'

export default {
  components: {
    AvatarModal,
    JUpload
  },
  data() {
    return {
      preview: {},
      dateFormat:"YYYY-MM-DD",
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      uploadLoading: false,
      confirmLoading: false,
      headers:{},
      form:this.$form.createForm(this),
      validatorRules: {
        realname: { rules: [{ required: true, message: '请输入用户名称!' }] },
        phone: { rules: [{ validator: this.validatePhone }] },
        email: {
          rules: [
            {
              validator: this.validateEmail
            }
          ]
        }
      },
      userInfo: {},
      url: {
        fileUpload: window._CONFIG['domianURL'] + '/sys/common/upload',
        userInfo: "/teaching/user/info",
        userHonor: "/teaching/teachingUserHonor/userHonor",
        editUser: "/teaching/user/edit",
        imgerver: window._CONFIG['domianURL'] + '/sys/common/static'
      }
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    moment,
    getUserInfo() {
      var that = this
      getAction(this.url.userInfo).then(res => {
        if (res.success) {
          that.userInfo = res.result
          that.$nextTick(() => {
            that.form.setFieldsValue(pick(that.userInfo, 'username', 'sex', 'realname', 'email', 'phone', 'avatar'))
          })
          //that.loadUserHonor(that.userInfo.id)
        }
      })
    },
    loadUserHonor(userId) {
      getAction(this.url.userHonor, { userId: userId }).then(res => {
        console.log(res)
        if (res.success) {
          this.selectedHonor = res.result
        } else {
          console.log(res.message)
        }
      })
    },
    handleSubmit() {
      const that = this
      // that.form.validateFields((err, values) => {
      //   console.log(err) 
      //   if (!err) {
          var values = that.form.getFieldsValue();
          console.log(values)
          that.confirmLoading = true
          // let avatar = that.userInfo.avatar
          if (!values.birthday) {
            values.birthday = ''
          } else {
            values.birthday = values.birthday.format(this.dateFormat)
          }
          let formData = Object.assign(this.userInfo, values)
          // formData.avatar = avatar
          // that.addDepartsToUser(that,formData); // 调用根据当前用户添加部门信息的方法
          let obj = putAction(that.url.editUser, formData).then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
      //   }
      // })
    },
    validatePhone(rule, value, callback) {
      if (!value) {
        callback()
      } else {
        if (new RegExp(/^1[3|4|5|7|8][0-9]\d{8}$/).test(value)) {
          var params = {
            tableName: 'sys_user',
            fieldName: 'phone',
            fieldVal: value,
            dataId: this.userId
          }
          duplicateCheck(params).then(res => {
            if (res.success) {
              callback()
            } else {
              callback('手机号已存在!')
            }
          })
        } else {
          callback('请输入正确格式的手机号码!')
        }
      }
    },
    validateEmail(rule, value, callback) {
      if (!value) {
        callback()
      } else {
        if (
          new RegExp(
            /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
          ).test(value)
        ) {
          var params = {
            tableName: 'sys_user',
            fieldName: 'email',
            fieldVal: value,
            dataId: this.userId
          }
          duplicateCheck(params).then(res => {
            console.log(res)
            if (res.success) {
              callback()
            } else {
              callback('邮箱已存在!')
            }
          })
        } else {
          callback('请输入正确格式的邮箱!')
        }
      }
    },
    normFile(e) {
      console.log('Upload event:', e)
      if (Array.isArray(e)) {
        return e
      }
      return e && e.fileList
    },
  }
}
</script>

<style lang="less" scoped>

.avatar-upload-wrapper {
  height: 200px;
  width: 100%;
}

.ant-upload-preview {
  position: relative;
  margin: 0 auto;
  width: 100%;
  max-width: 180px;
  border-radius: 50%;
  box-shadow: 0 0 4px #ccc;

  .upload-icon {
    position: absolute;
    top: 0;
    right: 10px;
    font-size: 1.4rem;
    padding: 0.5rem;
    background: rgba(222, 221, 221, 0.7);
    border-radius: 50%;
    border: 1px solid rgba(0, 0, 0, 0.2);
  }
  .mask {
    opacity: 0;
    position: absolute;
    background: rgba(0, 0, 0, 0.4);
    cursor: pointer;
    transition: opacity 0.4s;

    &:hover {
      opacity: 1;
    }

    i {
      font-size: 2rem;
      position: absolute;
      top: 50%;
      left: 50%;
      margin-left: -1rem;
      margin-top: -1rem;
      color: #d6d6d6;
    }
  }

  img,
  .mask {
    width: 100%;
    max-width: 180px;
    height: 100%;
    border-radius: 50%;
    overflow: hidden;
  }
}
</style>